package com.lvxh.exp.utils;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import com.lvxh.exp.bean.TableProperties;



public class BeanUtil
{
    public static void initJavaBean(String fileAboPath,  String tableName, String filePackage,List<TableProperties> plist)
    {
        String className = StringUtil.headToUpperCase(tableName);
        
        
        File dir = new File(fileAboPath+"/"+tableName + "/dao/holder/");
        if (!dir.exists())
        {
            dir.mkdirs();
        }
        File file = new File(fileAboPath+"/"+tableName +"/dao/holder/" + className + ".java");
        if (!file.exists())
        {
            try
            {
                file.createNewFile();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try
        {
            fos = new FileOutputStream(file);
            osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write("package " + filePackage+"." + tableName + ".dao.holder" + ";");
            osw.write(System.getProperty("line.separator"));

            osw.write("import java.util.Date;");
            osw.write(System.getProperty("line.separator"));

            osw.write("import " + filePackage + ".bean" + ".BaseHolder;");
            osw.write(System.getProperty("line.separator"));

            osw.write("public class " + className + " extends BaseHolder {");
            osw.write(System.getProperty("line.separator"));

            osw.write(" private static final long serialVersionUID = "
                    + new Random().nextLong() + "L;");
            osw.write(System.getProperty("line.separator"));

            for (int i = 0; i < plist.size(); i++)
            {
                String type = dbTypeToJavaType(plist.get(i).getType());
                osw.write("private " + type + " " + plist.get(i).getField()
                        + ";");
                osw.write(System.getProperty("line.separator"));
                osw.flush();
            }
            for (int i = 0; i < plist.size(); i++)
            {

                String type = dbTypeToJavaType(plist.get(i).getType());
                String field = plist.get(i).getField();
                osw.write("public " + type + " get"
                        + StringUtil.headToUpperCase(field) + "()");
                osw.write(System.getProperty("line.separator"));
                osw.write("{ ");
                osw.write(System.getProperty("line.separator"));

                osw.write("return " + field + ";");
                osw.write(System.getProperty("line.separator"));

                osw.write("}");
                osw.write(System.getProperty("line.separator"));

                osw.write("public void set" + StringUtil.headToUpperCase(field)
                        + "(" + type + " " + field + ")");
                osw.write(System.getProperty("line.separator"));
                osw.write("{ ");
                osw.write(System.getProperty("line.separator"));

                osw.write("this." + field + " = " + field + ";");
                osw.write(System.getProperty("line.separator"));

                osw.write("}");
                osw.write(System.getProperty("line.separator"));

                osw.flush();
            }

            osw.write("}");
            osw.flush();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            ExpUtils.closeStream(osw);
            ExpUtils.closeStream(fos);

        }
    }

    public static String dbTypeToJavaType(String type)
    {
        type = type.toLowerCase();
        if (type.contains("char"))
        {
            return "String";
        }
        if (type.contains("int"))
        {
            return "Integer";
        }
        if (type.contains("datetime"))
        {
            return "Date";
        }
        else
            return type;
    }

    public static void initMapperBean(String fileAboPath,  String tableName, String filePackage)
    {
        File dir = new File(fileAboPath+"/"+tableName + "/dao/");
        if (!dir.exists())
        {
            dir.mkdirs();
        }
        File file = new File(fileAboPath +"/"+tableName+ "/dao/"+StringUtil.headToUpperCase(tableName)+"Mapper.java");
        if (!file.exists())
        {
            try
            {

                file.createNewFile();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try
        {
            fos = new FileOutputStream(file);
            osw = new OutputStreamWriter(fos, "UTF-8");

            /**
             * package "+pack+".dao;
             * 
             * import java.util.List;
             * 
             * import "+pack+".dao.holder.ExpMenu;
             * 
             * public interface ExpMenuMapper { int deleteByPrimaryKey(Integer
             * public int delete(T param); public int insert(T param); public
             * int update(T param); int updateBySelective(T param); public
             * List<T> getList(T param);
             */
            osw.write("package " + filePackage+"."+ tableName+ ".dao;");
            osw.write(System.getProperty("line.separator"));

            osw.write("import java.util.List;");
            osw.write(System.getProperty("line.separator"));

            osw.write("import "+filePackage+"."
                    + tableName.toLowerCase(Locale.getDefault())
                    + ".dao.holder." + StringUtil.headToUpperCase(tableName)
                    + ";");
            osw.write(System.getProperty("line.separator"));

            osw.write("public interface " + StringUtil.headToUpperCase(tableName) + "Mapper" + "{");
            osw.write(System.getProperty("line.separator"));

            osw.write("int delete(" + StringUtil.headToUpperCase(tableName)
                    + " bean);");
            osw.write(System.getProperty("line.separator"));

            osw.write("int update(" + StringUtil.headToUpperCase(tableName)
                    + " bean);");
            osw.write(System.getProperty("line.separator"));

            osw.write("int insert(" + StringUtil.headToUpperCase(tableName)
                    + " bean);");
            osw.write(System.getProperty("line.separator"));

            osw.write("int getCountOfSummary("
                    + StringUtil.headToUpperCase(tableName) + " bean);");
            osw.write(System.getProperty("line.separator"));

            osw.write("List<" + StringUtil.headToUpperCase(tableName)
                    + "> getList(" + StringUtil.headToUpperCase(tableName)
                    + " bean);");
            osw.write(System.getProperty("line.separator"));

            osw.write("List<" + StringUtil.headToUpperCase(tableName)
                    + "> getListOfSummary("
                    + StringUtil.headToUpperCase(tableName) + " bean);");
            osw.write(System.getProperty("line.separator"));

            osw.write("}");
            osw.flush();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            ExpUtils.closeStream(osw);
            ExpUtils.closeStream(fos);

        }
    }

    public static void initServiceBean(String fileAboPath,  String tableName, String filePackage)
    {
        File dir = new File(fileAboPath+"/"+ tableName + "/service/");
        if (!dir.exists())
        {
            dir.mkdirs();
        }
        File file = new File(fileAboPath+"/"+ tableName + "/service/"+StringUtil.headToUpperCase(tableName)+"Service.java");
        if (!file.exists())
        {
            try
            {

                file.createNewFile();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try
        {
            fos = new FileOutputStream(file);
            osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write(getServiceBean(tableName,filePackage));
         
//            osw.write("package " + filePackage + ".service;");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("import java.util.List;");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("import "+pack+"."
//                    + className.toLowerCase(Locale.getDefault())
//                    + ".dao.holder." + StringUtil.headToUpperCase(className)
//                    + ";");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("public interface " + className + "Service" + "{");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("int delete(" + StringUtil.headToUpperCase(className)
//                    + " bean);");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("int update(" + StringUtil.headToUpperCase(className)
//                    + " bean);");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("int insert(" + StringUtil.headToUpperCase(className)
//                    + " bean);");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("int getCountOfSummary("
//                    + StringUtil.headToUpperCase(className) + " bean);");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("List<" + StringUtil.headToUpperCase(className)
//                    + "> getList(" + StringUtil.headToUpperCase(className)
//                    + " bean);");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("List<" + StringUtil.headToUpperCase(className)
//                    + "> getListOfSummary("
//                    + StringUtil.headToUpperCase(className) + " bean);");
//            osw.write(System.getProperty("line.separator"));

//            osw.write("}");
            osw.flush();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            ExpUtils.closeStream(osw);
            ExpUtils.closeStream(fos);

        }
    }
    public static void initImplBean(String fileAboPath,  String tableName, String filePackage)
    {
        File dir = new File(fileAboPath+"/"+tableName + "/service/impl/");
        if (!dir.exists())
        {
            dir.mkdirs();
        }
        File file = new File(fileAboPath+"/"+tableName + "/service/impl/"+StringUtil.headToUpperCase(tableName)+"ServiceImpl.java");
        if (!file.exists())
        {
            try
            {

                file.createNewFile();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try
        {
            fos = new FileOutputStream(file);
            osw = new OutputStreamWriter(fos, "UTF-8");
           
            osw.write(getServiceImplBean(tableName, filePackage));
            
            osw.flush();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            ExpUtils.closeStream(osw);
            ExpUtils.closeStream(fos);

        }
    }
    public static void initController(String fileAboPath,  String tableName, String filePackage)
    {
        File dir = new File(fileAboPath+"/"+tableName + "/controller/");
        if (!dir.exists())
        {
            dir.mkdirs();
        }
        File file = new File(fileAboPath+"/"+tableName + "/controller/"+StringUtil.headToUpperCase(tableName)+"Controller.java");
        if (!file.exists())
        {
            try
            {
                file.createNewFile();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try
        {
            fos = new FileOutputStream(file);
            osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write(getControllerFram(filePackage, tableName));
//            osw.write("package " + controllerPackage + ".controller;");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("import " + filePackage + ".bean.BaseController;");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("import " + filePackage + ".bean.GridResultHolder;");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("import " + filePackage + "." + className.toLowerCase()
//                    + ".service." + className + "Service;");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("import " + filePackage + "." + className.toLowerCase()
//                    + ".dao.holder." + className + ";");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("import org.apache.log4j.Logger;");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("import org.springframework.beans.factory.annotation.Autowired;");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("import org.springframework.stereotype.Controller;");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("import org.springframework.web.bind.annotation.RequestMapping;");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("import org.springframework.web.bind.annotation.ResponseBody;");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("import org.springframework.web.servlet.ModelAndView;");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("@RequestMapping(\"/" + className.toLowerCase() + "\")");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("@Controller");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("public class " + className
//                    + "Controller extends BaseController {");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("private static final Logger log = Logger.getLogger("
//                    + className + "Controller.class);");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("@Autowired");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("private " + StringUtil.headToUpperCase(className)
//                    + "Service " + className.toLowerCase() + "Service;");
//            osw.write(System.getProperty("line.separator"));
//
//            /**
//             * @RequestMapping("/mysql") public ModelAndView initSql() {
//             *                           log.info("start method  initSql");
//             *                           return new
//             *                           ModelAndView("document/sql");
//             * 
//             *                           }
//             */
//            osw.write("@RequestMapping(\"/init" + className.toLowerCase()
//                    + "\")");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("public ModelAndView init" + className + "(){");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("log.info(\"start method init" + className + "\");");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("log.info(\"end method init" + className + "\");");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("return new ModelAndView(\"" + className.toLowerCase()
//                    + "/view" + className + "\");");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("}");
//            osw.write(System.getProperty("line.separator"));
//
//            /**
//             * @RequestMapping("/getGridSql")
//             * @ResponseBody public GridResultHolder getGridSql(Sequence
//             *               seq,String seqname) {
//             *               log.info("start method  initSql"); int count
//             *               =sequenceService.getCountOfSummary(seq); int sum =
//             *               procesPage(seq, count); GridResultHolder result =
//             *               new GridResultHolder();
//             *               result.setResult(sequenceService
//             *               .getListOfSummary(seq)); result.setPageCount(sum);
//             *               return result;
//             * 
//             *               }
//             */
//            osw.write("@RequestMapping(\"/getGrid\")");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("@ResponseBody");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("public GridResultHolder getGrid(" + className
//                    + " bean){");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("log.info(\"start method getGrid\");");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write(" GridResultHolder result = new GridResultHolder();");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("int count =" + className.toLowerCase()
//                    + "Service.getCountOfSummary(bean);");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("int sum = procesPage(bean, count);");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("result.setResult(" + className.toLowerCase()
//                    + "Service.getListOfSummary(bean));");
//            osw.write(System.getProperty("line.separator"));
//            osw.write("result.setPageCount(sum);");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("log.info(\"end method getGrid\");");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("return result;");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("}");
//            osw.write(System.getProperty("line.separator"));
//
//            osw.write("}");
            osw.flush();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            ExpUtils.closeStream(osw);
            ExpUtils.closeStream(fos);

        }
    }
    public static String getControllerFram(String pack,String tableName)
    {
        StringBuffer buf = new StringBuffer();
        buf.append("package "+pack+"."+tableName+".controller;");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("import java.util.ArrayList;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import java.util.HashMap;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import java.util.List;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import java.util.Map;");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("import org.apache.log4j.Logger;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import org.springframework.beans.factory.annotation.Autowired;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import org.springframework.stereotype.Controller;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import org.springframework.web.bind.annotation.RequestMapping;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import org.springframework.web.bind.annotation.ResponseBody;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import org.springframework.web.servlet.ModelAndView;");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("import "+pack+".bean.BaseController;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import "+pack+".bean.GridResultHolder;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import "+pack+"."+tableName+".dao.holder."+StringUtil.headToUpperCase(tableName)+";");
        buf.append(System.getProperty("line.separator"));
        buf.append("import "+pack+"."+tableName+".service."+StringUtil.headToUpperCase(tableName)+"Service;");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("@RequestMapping(\"/"+tableName+"\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("@Controller");
        buf.append(System.getProperty("line.separator"));
        buf.append("public class "+StringUtil.headToUpperCase(tableName)+"Controller extends BaseController");
        buf.append(System.getProperty("line.separator"));
        buf.append("{");
        buf.append(System.getProperty("line.separator"));
        buf.append("    private static final Logger log = Logger.getLogger("+StringUtil.headToUpperCase(tableName)+"Controller.class);");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @Autowired");
        buf.append(System.getProperty("line.separator"));
        buf.append("    private "+StringUtil.headToUpperCase(tableName)+"Service "+tableName+"Service;");
        buf.append(System.getProperty("line.separator"));
        List<String> list = getControllerMethod(tableName);
        for(int i=0;i<list.size();i++)
        {
            buf.append(list.get(i));
        }
     
        buf.append(System.getProperty("line.separator"));
        buf.append("}");
        buf.append(System.getProperty("line.separator"));
        
        return buf.toString();

    }
    public static List<String> getControllerMethod(String tableName)
    {
        List<String> list = new ArrayList<String>();
        list.add(getInitView(tableName));
        list.add(getGrid(tableName));
        list.add(getInitAdd(tableName));
        list.add(getAdd(tableName));
        list.add(getInitModify(tableName));
        list.add(getModify(tableName));
        list.add(getDel(tableName));
        list.add(getDelAll(tableName));
        return list;
    }
    public static String getInitView(String tableName)
    {
        StringBuffer buf = new StringBuffer();
        buf.append(System.getProperty("line.separator"));
        buf.append("    @RequestMapping(\"/init"+tableName+"\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public ModelAndView init"+StringUtil.headToUpperCase(tableName)+"()");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"start method init"+StringUtil.headToUpperCase(tableName)+"\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"end method init"+StringUtil.headToUpperCase(tableName)+"\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return new ModelAndView(\""+tableName+"/view"+StringUtil.headToUpperCase(tableName)+"\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        return buf.toString();
    }
    
    public static String getGrid(String tableName)
    {
        StringBuffer buf = new StringBuffer();
        buf.append(System.getProperty("line.separator"));
        buf.append("    @RequestMapping(\"/getGrid\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @ResponseBody");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public GridResultHolder getGrid("+StringUtil.headToUpperCase(tableName)+" bean)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"start method getGrid\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        GridResultHolder result = new GridResultHolder();");
        buf.append(System.getProperty("line.separator"));
        buf.append("        int count = "+tableName+"Service.getCountOfSummary(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        int sum = procesPage(bean, count);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        result.setResult("+tableName+"Service.getListOfSummary(bean));");
        buf.append(System.getProperty("line.separator"));
        buf.append("        result.setPageCount(sum);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"end method getGrid\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return result;");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        return buf.toString();
    }
    
    public static String getInitAdd(String tableName)
    {
        StringBuffer buf = new StringBuffer();
        buf.append(System.getProperty("line.separator"));
        buf.append("    @RequestMapping(\"/initadd\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public ModelAndView initAdd()");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"start method initAdd\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"end method initAdd\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return new ModelAndView(\""+tableName+"/add"+StringUtil.headToUpperCase(tableName)+"\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        return buf.toString();
    }
    
    public static String getAdd(String tableName)
    {
        StringBuffer buf = new StringBuffer();
        buf.append(System.getProperty("line.separator"));
        buf.append("    @RequestMapping(\"/add\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public ModelAndView add("+StringUtil.headToUpperCase(tableName)+" bean)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        int result=0;");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"start method add\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        Map<String,String>map = new HashMap<String,String>();");
        buf.append(System.getProperty("line.separator"));
        buf.append("       ");
        buf.append(System.getProperty("line.separator"));
        buf.append("        try");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            result = "+tableName+"Service.insert(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        catch (Exception e)");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.error(e.getMessage(),e);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        if(result > 0)");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            map.put(\"backUrl\", \"/"+tableName+"/init"+tableName+".action\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method add\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\"global/success\",map);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        else");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method add\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\"global/failed\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("       ");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        return buf.toString();
    }
    
    public static String getInitModify(String tableName)
    {
        StringBuffer buf = new StringBuffer();
        buf.append(System.getProperty("line.separator"));
        buf.append("    @RequestMapping(\"/initmodify\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public ModelAndView initModify(String delFlag)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"start method initModify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        Map<String,Object>map = new HashMap<String,Object>();");
        buf.append(System.getProperty("line.separator"));
        buf.append("        "+StringUtil.headToUpperCase(tableName)+" bean = new "+StringUtil.headToUpperCase(tableName)+"();");
        buf.append(System.getProperty("line.separator"));
        buf.append("        bean.setId(Integer.parseInt(delFlag));");
        buf.append(System.getProperty("line.separator"));
        buf.append("        List <"+StringUtil.headToUpperCase(tableName)+">list = "+tableName+"Service.getList(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        if(list == null || list.isEmpty())");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method initModify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\"global/failed\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        else");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            map.put(\"bean\", list.get(0));");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method initModify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\""+tableName+"/modify"+StringUtil.headToUpperCase(tableName)+"\",map);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        buf.append(System.getProperty("line.separator"));
        return buf.toString();
    }
    
    public static String getModify(String tableName)
    {
        StringBuffer buf = new StringBuffer();
        buf.append(System.getProperty("line.separator"));
        buf.append("    @RequestMapping(\"/modify\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public ModelAndView modify("+StringUtil.headToUpperCase(tableName)+" bean)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        int result=0;");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"start method modify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        Map<String,String>map = new HashMap<String,String>();");
        buf.append(System.getProperty("line.separator"));
        buf.append("       ");
        buf.append(System.getProperty("line.separator"));
        buf.append("        try");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            result = "+tableName+"Service.update(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        catch (Exception e)");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.error(e.getMessage(),e);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        if(result > 0)");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            map.put(\"backUrl\", \"/"+tableName+"/init"+tableName+".action\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method modify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\"global/success\",map);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        else");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method modify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\"global/failed\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("       ");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        return buf.toString();
    }
    public static String getDel(String tableName)
    {
        StringBuffer buf = new StringBuffer();
        buf.append(System.getProperty("line.separator"));
        buf.append("    @RequestMapping(\"/del\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public ModelAndView del(String delFlag)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        int result=0;");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"start method modify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        Map<String,String>map = new HashMap<String,String>();");
        buf.append(System.getProperty("line.separator"));
        buf.append("       ");
        buf.append(System.getProperty("line.separator"));
        buf.append("        try");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            if(delFlag !=null && !\"\".equals(delFlag))");
        buf.append(System.getProperty("line.separator"));
        buf.append("            {");
        buf.append(System.getProperty("line.separator"));
        buf.append("                "+StringUtil.headToUpperCase(tableName)+" bean = new "+StringUtil.headToUpperCase(tableName)+"();");
        buf.append(System.getProperty("line.separator"));
        buf.append("                bean.setId(Integer.parseInt(delFlag.split(\"\\\\|\")[1]));");
        buf.append(System.getProperty("line.separator"));
        buf.append("                result = "+tableName+"Service.delete(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("            }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        catch (Exception e)");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.error(e.getMessage(),e);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        if(result > 0)");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            map.put(\"backUrl\", \"/"+tableName+"/init"+tableName+".action\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method modify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\"global/success\",map);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        else");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method modify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\"global/failed\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("       ");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        return buf.toString();
    }
    public static String getDelAll(String tableName)
    {
        StringBuffer buf = new StringBuffer();
        buf.append("    @RequestMapping(\"/delall\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public ModelAndView delAll(String delFlag)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        ");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("        int result=0;");
        buf.append(System.getProperty("line.separator"));
        buf.append("        log.info(\"start method modify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        Map<String,String>map = new HashMap<String,String>();");
        buf.append(System.getProperty("line.separator"));
        buf.append("       ");
        buf.append(System.getProperty("line.separator"));
        buf.append("        try");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            if(delFlag !=null && !\"\".equals(delFlag))");
        buf.append(System.getProperty("line.separator"));
        buf.append("            {");
        buf.append(System.getProperty("line.separator"));
        buf.append("                ");
        buf.append(System.getProperty("line.separator"));
        buf.append("                List<"+StringUtil.headToUpperCase(tableName)+"> list = new ArrayList<"+StringUtil.headToUpperCase(tableName)+">();");
        buf.append(System.getProperty("line.separator"));
        buf.append("                String str[]= delFlag.split(\"\\\\|\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("                for(int i=1;i<str.length;i++)");
        buf.append(System.getProperty("line.separator"));
        buf.append("                {");
        buf.append(System.getProperty("line.separator"));
        buf.append("                    "+StringUtil.headToUpperCase(tableName)+" bean = new "+StringUtil.headToUpperCase(tableName)+"();");
        buf.append(System.getProperty("line.separator"));
        buf.append("                    bean.setId(Integer.parseInt(str[i]));");
        buf.append(System.getProperty("line.separator"));
        buf.append("                    list.add(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("                }");
        buf.append(System.getProperty("line.separator"));
        buf.append("                result = "+tableName+"Service.delete(list);");
        buf.append(System.getProperty("line.separator"));
        buf.append("            }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        catch (Exception e)");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.error(e.getMessage(),e);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        if(result > 0)");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            map.put(\"backUrl\", \"/"+tableName+"/init"+tableName+".action\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method modify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\"global/success\",map);");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        else");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            log.info(\"end method modify\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            return new ModelAndView(\"global/failed\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("       ");
        buf.append(System.getProperty("line.separator"));
        buf.append("    ");
        buf.append(System.getProperty("line.separator"));
        buf.append("       ");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        return buf.toString();
    }
    public static String getServiceBean(String tableName,String pack)
    {
        StringBuffer buf = new StringBuffer();
        buf.append("package "+pack+"."+tableName+".service;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import java.util.List;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import "+pack+"."+tableName+".dao.holder."+StringUtil.headToUpperCase(tableName)+";");
        buf.append(System.getProperty("line.separator"));
        buf.append("public interface "+StringUtil.headToUpperCase(tableName)+"Service{");
        buf.append(System.getProperty("line.separator"));
        buf.append("int delete("+StringUtil.headToUpperCase(tableName)+" bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("int delete(List<"+StringUtil.headToUpperCase(tableName)+"> list);");
        buf.append(System.getProperty("line.separator"));
        buf.append("int update("+StringUtil.headToUpperCase(tableName)+" bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("int insert("+StringUtil.headToUpperCase(tableName)+" bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("int getCountOfSummary("+StringUtil.headToUpperCase(tableName)+" bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("List<"+StringUtil.headToUpperCase(tableName)+"> getList("+StringUtil.headToUpperCase(tableName)+" bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("List<"+StringUtil.headToUpperCase(tableName)+"> getListOfSummary("+StringUtil.headToUpperCase(tableName)+" bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("}");
        buf.append(System.getProperty("line.separator"));
        return buf.toString();
    }
    public static String getServiceImplBean(String tableName,String pack)
    {
        StringBuffer buf = new StringBuffer();
        buf.append("package "+pack+"."+tableName+".service.impl;");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("import java.util.List;");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("import "+pack+".bean.ExpException;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import "+pack+"."+tableName+".dao.holder."+StringUtil.headToUpperCase(tableName)+";");
        buf.append(System.getProperty("line.separator"));
        buf.append("import "+pack+"."+tableName+".dao."+StringUtil.headToUpperCase(tableName)+"Mapper;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import "+pack+"."+tableName+".service."+StringUtil.headToUpperCase(tableName)+"Service;");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("import org.springframework.beans.factory.annotation.Autowired;");
        buf.append(System.getProperty("line.separator"));
        buf.append("import org.springframework.stereotype.Service;");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("@Service(\""+tableName+"Service\")");
        buf.append(System.getProperty("line.separator"));
        buf.append("public class "+StringUtil.headToUpperCase(tableName)+"ServiceImpl implements "+StringUtil.headToUpperCase(tableName)+"Service");
        buf.append(System.getProperty("line.separator"));
        buf.append("{");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @Autowired");
        buf.append(System.getProperty("line.separator"));
        buf.append("    private "+StringUtil.headToUpperCase(tableName)+"Mapper mapper;");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @Override");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public int delete("+StringUtil.headToUpperCase(tableName)+" bean)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return mapper.delete(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @Override");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public int update("+StringUtil.headToUpperCase(tableName)+" bean)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return mapper.update(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @Override");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public int insert("+StringUtil.headToUpperCase(tableName)+" bean)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return mapper.insert(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @Override");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public int getCountOfSummary("+StringUtil.headToUpperCase(tableName)+" bean)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return mapper.getCountOfSummary(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @Override");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public List<"+StringUtil.headToUpperCase(tableName)+"> getList("+StringUtil.headToUpperCase(tableName)+" bean)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return mapper.getList(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @Override");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public List<"+StringUtil.headToUpperCase(tableName)+"> getListOfSummary("+StringUtil.headToUpperCase(tableName)+" bean)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return mapper.getListOfSummary(bean);");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        buf.append(System.getProperty("line.separator"));
        buf.append("");
        buf.append(System.getProperty("line.separator"));
        buf.append("    @Override");
        buf.append(System.getProperty("line.separator"));
        buf.append("    public int delete(List<"+StringUtil.headToUpperCase(tableName)+"> list)");
        buf.append(System.getProperty("line.separator"));
        buf.append("    {");
        buf.append(System.getProperty("line.separator"));
        buf.append("        for (int i = 0; i < list.size(); i++)");
        buf.append(System.getProperty("line.separator"));
        buf.append("        {");
        buf.append(System.getProperty("line.separator"));
        buf.append("            if (mapper.delete(list.get(i)) < 1)");
        buf.append(System.getProperty("line.separator"));
        buf.append("            {");
        buf.append(System.getProperty("line.separator"));
        buf.append("                new ExpException(\"del \" + list.get(i) + \" error\");");
        buf.append(System.getProperty("line.separator"));
        buf.append("            }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        }");
        buf.append(System.getProperty("line.separator"));
        buf.append("        return 1;");
        buf.append(System.getProperty("line.separator"));
        buf.append("    }");
        buf.append(System.getProperty("line.separator"));
        buf.append("}");
        buf.append(System.getProperty("line.separator"));
        return buf.toString();
    }
}
